package com.example.itemtouchhelperdemo;

import android.view.HapticFeedbackConstants;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.itemtouchhelperdemo.util.ImageLoadUtils;
import com.example.itemtouchhelperdemo.util.ToastUtil;
import com.example.itemtouchhelperdemo.util.Utils;

import java.util.ArrayList;
import java.util.List;

public class AvatarAdapter extends RecyclerView.Adapter<AvatarAdapter.AvatarViewHolder> {

    private final OnItemListener onItemListener;
    private List<Integer> avatars;

    /**
     * 当前选择位置
     */
    private int currentIndex;
    /**
     * 长按是所选中的位置
     */
    private int currentLongClickIndex = -1;
    private final RequestOptions requestOptions;

    public AvatarAdapter(ArrayList<Integer> avatars,OnItemListener onItemListener) {
        this.avatars = avatars;
        this.onItemListener = onItemListener;
        requestOptions = new RequestOptions();
        RequestOptions.bitmapTransform(new RoundedCorners((int) Utils.dpToPixel(8)));
        requestOptions.placeholder(R.drawable.icon_disconnect);
        requestOptions.diskCacheStrategy(DiskCacheStrategy.NONE);
        requestOptions.transform(new MultiTransformation(new CenterCrop(), new RoundedCorners((int) Utils.dpToPixel(8))));
    }

    @NonNull
    @Override
    public AvatarViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AvatarViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_avatar, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull AvatarViewHolder holder, int pos) {
        final int position = holder.getLayoutPosition();
        holder.itemView.setVisibility(View.VISIBLE);
        holder.mItemImg.setVisibility(View.VISIBLE);
        if (currentLongClickIndex == position) {
            holder.mFrameView.setBackgroundResource(R.drawable.main_item_long_selected);
        } else {
            holder.mFrameView.setBackgroundResource(R.drawable.main_item_select);
        }
        holder.mFrameView.setVisibility(currentIndex == position || currentLongClickIndex == position ? View.VISIBLE : View.INVISIBLE);
        ImageLoadUtils.loadImageWithTransForm(avatars.get(position), holder.mItemImg, requestOptions);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentIndex == position) {
                    return;
                }
                if (onItemListener != null) {
                    notifySelectItemChanged(position);
                    onItemListener.onClickAvatar(position);
                }
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (position == currentIndex) {
                    ToastUtil.showCenterToast(holder.itemView.getContext(), "不能删除选中的模型");
                    return true;
                }
                currentLongClickIndex = position;
                notifyItemChanged(position);
                v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
                v.post(new Runnable() {
                    @Override
                    public void run() {
                        if (onItemListener != null) {
                            onItemListener.onLongClick(v, holder, position);
                        }
                    }
                });
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return avatars == null ? 0 : avatars.size();
    }

    public void notifySelectItemChanged(int position) {
        notifyItemChanged(currentIndex);
        notifyItemChanged(position);
        currentIndex = position;
    }

    public void resetLongClickIndex(boolean needRefresh) {
        int temp = currentLongClickIndex;
        currentLongClickIndex = -1;
        if (needRefresh) {
            notifyItemChanged(temp);
        }
    }

    public static class AvatarViewHolder extends RecyclerView.ViewHolder {

        ImageView mItemImg;
        View mFrameView;

        public AvatarViewHolder(View itemView) {
            super(itemView);
            mItemImg = itemView.findViewById(R.id.bottom_item_img);
            mFrameView = itemView.findViewById(R.id.bottom_item_frame);
        }
    }


    public interface OnItemListener {

        void onLongClick(View v, AvatarViewHolder holder, int position);

        void onClickAvatar(int pos);
    }
}
